/*
 * Copyright (C) 2011 Dirk Weißenborn, dirk.weissenborn@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.weissenborn.colabtex.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.ListBox;
import com.weissenborn.colabtex.client.places.Dashboard;
import com.google.gwt.event.dom.client.ChangeEvent;

/**
 * The Class GroupManagerViewImpl.
 */
public class GroupManagerViewImpl extends Composite implements GroupManagerView {

	/**
	 * The Interface Binder.
	 */
	interface Binder extends UiBinder<Widget, GroupManagerViewImpl> {
	}
	
	/** The Constant binder. */
	private static final Binder binder = GWT.create(Binder.class);

	/** The listener. */
	private Presenter listener;
	
	/** The add member btn. */
	@UiField Button addMemberBtn;
	
	/** The ok btn. */
	@UiField Button okBtn;
	
	/** The delete member btn. */
	@UiField Button deleteMemberBtn;
	
	/** The delete group btn. */
	@UiField Button deleteGroupBtn;
	
	/** The member name box. */
	@UiField TextBox memberNameBox;
	
	/** The permission box. */
	@UiField ListBox permissionBox;
	
	/** The cancel btn. */
	@UiField Button cancelBtn;
	
	/** The group name box. */
	@UiField ListBox groupNameBox;
	
	/** The pad list. */
	@UiField ListBox padList;
	
	/** The pad box. */
	@UiField TextBox padBox;
	
	/** The add pad btn. */
	@UiField Button addPadBtn;
	
	/** The delete pad btn. */
	@UiField Button deletePadBtn;
	@UiField ListBox memberList;

	/**
	 * Instantiates a new group manager view impl.
	 */
	public GroupManagerViewImpl() {
		initWidget(binder.createAndBindUi(this));
		permissionBox.addItem("Editor");
		permissionBox.addItem("Admin");
		permissionBox.addItem("Read-only");
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.GroupManagerView#setPresenter(com.weissenborn.colabtex.client.view.GroupManagerView.Presenter)
	 */
	public void setPresenter(Presenter listener) {
		this.listener = listener;
	}

	/**
	 * On add member btn click.
	 *
	 * @param event the event
	 */
	@UiHandler("addMemberBtn")
	void onAddMemberBtnClick(ClickEvent event) {
		if(memberNameBox.getText()!=""&&permissionBox.getSelectedIndex()>=0) {
			listener.addMember(memberNameBox.getText(),permissionBox.getItemText(permissionBox.getSelectedIndex()),getGroupName());
		}
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.GroupManagerView#addMember(java.lang.String, java.lang.String)
	 */
	public void addMember(String name, String role) {
		memberList.addItem(name+"~"+role);
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.GroupManagerView#getGroupNameBox()
	 */
	public ListBox getGroupNameBox() {
		return groupNameBox;
	}
	
	/**
	 * Gets the member name box.
	 *
	 * @return the member name box
	 */
	public TextBox getMemberNameBox() {
		return memberNameBox;
	}

	/**
	 * Gets the permission box.
	 *
	 * @return the permission box
	 */
	public ListBox getPermissionBox() {
		return permissionBox;
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.GroupManagerView#getGroupName()
	 */
	public String getGroupName() {
		if(groupNameBox.getSelectedIndex()<0)
			return null;
		return groupNameBox.getItemText(groupNameBox.getSelectedIndex());
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.GroupManagerView#getPermission()
	 */
	public String getPermission() {
		return permissionBox.getItemText(permissionBox.getSelectedIndex());
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.GroupManagerView#getMemberName()
	 */
	public String getMemberName() {
		return memberNameBox.getText();
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.GroupManagerView#getSelectedMember()
	 */
	public String getSelectedMember() {
		return memberList.getItemText(memberList.getSelectedIndex()).split("~")[0];
	}
	
	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.GroupManagerView#getMemberList()
	 */
	public ListBox getMemberList() {
		return memberList;
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.GroupManagerView#getPadList()
	 */
	public ListBox getPadList() {
		return padList;
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.GroupManagerView#getPadName()
	 */
	public String getPadName() {
		return padBox.getText();
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.GroupManagerView#getSelectedPad()
	 */
	public String getSelectedPad() {
		return padList.getItemText(padList.getSelectedIndex());
	}	
	
	/**
	 * On ok btn click.
	 *
	 * @param event the event
	 */
	@UiHandler("okBtn")
	void onOkBtnClick(ClickEvent event) {
		listener.goTo(new Dashboard());
	}
	
	/**
	 * On cancel btn click.
	 *
	 * @param event the event
	 */
	@UiHandler("cancelBtn")
	void onCancelBtnClick(ClickEvent event) {
		listener.goTo(new Dashboard());
	}
	
	/**
	 * On delete group btn click.
	 *
	 * @param event the event
	 */
	@UiHandler("deleteGroupBtn")
	void onDeleteGroupBtnClick(ClickEvent event) {
		listener.deleteGroup();
	}
	
	/**
	 * On add pad btn click.
	 *
	 * @param event the event
	 */
	@UiHandler("addPadBtn")
	void onAddPadBtnClick(ClickEvent event) {
		listener.addGroupPad();
	}
	
	/**
	 * On group name box change.
	 *
	 * @param event the event
	 */
	@UiHandler("groupNameBox")
	void onGroupNameBoxChange(ChangeEvent event) {
		listener.fillPads();
		listener.fillMembers();
	}
	
	/**
	 * On delete pad btn click.
	 *
	 * @param event the event
	 */
	@UiHandler("deletePadBtn")
	void onDeletePadBtnClick(ClickEvent event) {
		listener.deleteGroupPad(getSelectedPad(), getGroupName());
		padList.removeItem(padList.getSelectedIndex());
	}
	
	/**
	 * On delete member btn click.
	 *
	 * @param event the event
	 */
	@UiHandler("deleteMemberBtn")
	void onDeleteMemberBtnClick(ClickEvent event) {
		listener.deleteMember(getSelectedMember(), getGroupName());
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.GroupManagerView#deleteMember(java.lang.String)
	 */
	public void deleteMember(String name) {
		for (int i = 0; i < memberList.getItemCount(); i++) {
			if(memberList.getItemText(i).contains(name)) {
				memberList.removeItem(i);
				break;
			}
		}
	}

	/**
	 * On member name box change.
	 *
	 * @param event the event
	 */
	@UiHandler("memberNameBox")
	void onMemberNameBoxChange(ChangeEvent event) {
		listener.fillMembers();
		listener.fillPads();
	}
}
